package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.BrandCategoryMapper;
import cn.tedu.csmall.product.mapper.CategoryAttributeTemplateMapper;
import cn.tedu.csmall.product.mapper.CategoryMapper;
import cn.tedu.csmall.product.pojo.entity.Brand;
import cn.tedu.csmall.product.pojo.entity.BrandCategory;
import cn.tedu.csmall.product.pojo.entity.Category;
import cn.tedu.csmall.product.pojo.entity.CategoryAttributeTemplate;
import cn.tedu.csmall.product.pojo.param.CategoryAddNewParam;
import cn.tedu.csmall.product.pojo.param.CategoryUpdateInfoParam;
import cn.tedu.csmall.product.service.CategoryService;
import cn.tedu.csmall.product.web.ServiceCode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private CategoryAttributeTemplateMapper categoryAttributeTemplateMapper;

    @Override
    //添加类别
    public void addNew(CategoryAddNewParam categoryAddNewParam) {
        log.debug("开始处理[添加类别]的业务,参数:{}",categoryAddNewParam);
        //检查类别名称是否被占用,如果被占用,则抛出异常
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        //name='参数中的类别名称'
        queryWrapper.eq("name", categoryAddNewParam.getName());
        int countByName = categoryMapper.selectCount(queryWrapper);
        if (countByName > 0) {
            String message = "添加类别失败,类别名称已经被占用!!!";
            /* System.out.println(message);*/
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        //将类别数据写入到数据库中
        Category category = new Category();
        BeanUtils.copyProperties(categoryAddNewParam, category);
        category.setGmtCreate(LocalDateTime.now());
        category.setGmtModified(LocalDateTime.now());
        int rows = categoryMapper.insert(category);
        if (rows != 1) {
            String message = "添加管理员失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        //categoryMapper.insert(category);
        log.debug("将新的类别数据插入到数据库,完成!");
    }

    @Override
    //删除类别
    public void delete(Long id) {
        log.debug("开始处理【删除类别】的业务,参数:{}", id);
        // 检查类别是否存在，如果不存在，则抛出异常
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        int countById = categoryMapper.selectCount(queryWrapper);
        log.debug("根据类别ID统计匹配的类别数量，结果：{}", countById);
        if (countById == 0) {
            String message = "删除类别失败，类别数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);

        }

        // 检查是否有属性模板关联到了此类别，如果存在，则抛出异常
        QueryWrapper<CategoryAttributeTemplate> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("attribute_template_id", id);
        int countByAttributeTemplateId = categoryAttributeTemplateMapper.selectCount(queryWrapper2);
        log.debug("根据属性模板ID统计匹配的属性模板数量，结果：{}", countById);
        if (countByAttributeTemplateId > 0) {
            String message = "删除类别模板失败，仍有属性模板关联到此类别！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        int rows = categoryMapper.deleteById(id);
        if (rows != 1) {
            String message = "添加管理员失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
        //categoryMapper.deleteById(id);
    }

    @Override
    //修改类别
    public void updateById(Long id, CategoryUpdateInfoParam categoryUpdateInfoParam) {
        log.debug("开始处理[修改类别]的业务,参数:{}",categoryUpdateInfoParam);

        //检查类别名称是否被占用,如果被占用,则抛出异常
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        //name='参数中的类别名称'
        queryWrapper.eq("name", categoryUpdateInfoParam.getName());
        int countByName = categoryMapper.selectCount(queryWrapper);

        if (countByName > 0) {
            String message = "修改类别失败,类别名称已经被占用!!!";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Category category = new Category();
        BeanUtils.copyProperties(categoryUpdateInfoParam, category);
        category.setId(id);
        category.setGmtCreate(LocalDateTime.now());
        category.setGmtModified(LocalDateTime.now());
        int rows = categoryMapper.updateById(category);
        if (rows != 1) {
            String message = "添加管理员失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
        //categoryMapper.updateById(category);
    }
}
